package models.user;

import me.chanjar.weixin.mp.bean.result.WxMpUser;
import models.UUIDModel;
import models.campus.Campus;
import models.constants.DeletedStatus;
import models.constants.Gender;
import models.enums.UserSource;

import javax.persistence.*;
import java.util.Date;

/**
 * 微信用户
 */
@Entity
@Table(name = "ur_users")
public class User extends UUIDModel {

    public static final String WXMP_CACHEKEY  = "WxMpUser2_";

    /**
     * 用户昵称
     */
    @Column(name = "nick_name", length = 10)
    public String nickName;

    /**
     * 真实姓名
     */
    @Column(name="real_name", length = 10)
    public String realName;

    /**
     * 用户来源
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "user_score", length = 10)
    public UserSource userSource;

    /**
     * 用户来源编号
     */
    @Column(name = "soure_uuid" , length = 32)
    public String soureUuid;

    /**
     * 手机号
     */
    @Column(name="phone", length = 11)
    public String phone;

    /**
     * 身份证号
     */
    @Column(name="id_card", length = 20)
    public String idCard;

    /**
     * 性别 0 男, 1 女, 2 未知
     */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "gender")
    public Gender gender;

    /**
     * 微信用户名，fromUserName.
     * TODO: 加上索引.
     */
    @Column(name = "openId", length = 50)
    public String openId;

    /**
     * 年龄
     */
    @Column(name="age")
    public Integer age;

    /**
     * 备注信息
     */
    @Column(name = "remark", length = 100)
    public String remark;

    public User(){super();}

    public User(String realName, String phone, String idCard, Gender gender, Integer age){
        this.realName = realName;
        this.phone = phone;
        this.idCard = idCard;
        this.gender = gender;
        this.age = age;
    }

    /**
     *
     * @param id
     * @return
     */
    public static User findByUserId(String id){
        return User.find(" id = ? and deleted = ?" ,id , DeletedStatus.UN_DELETED).first();
    }

    /**
     * 查询出商户指定OpenId的用户.
     */
    public static User findOrCreateMerchantWxUser(Campus campus, WxMpUser wxMpUser , String dcode) {
        User weixinUser = User.find("weixinData = ? and openId=? and deleted = ?", campus.weixinData , wxMpUser.getOpenId() , DeletedStatus.UN_DELETED).first();
        if (weixinUser == null) {
            weixinUser = new User();
            weixinUser.createdAt = new Date();
            weixinUser.deleted = DeletedStatus.UN_DELETED;
//            weixinUser.weixinData = campus.weixinData;
            weixinUser.openId = wxMpUser.getOpenId();
            weixinUser.nickName = wxMpUser.getNickname();
//            weixinUser.subcribed = Boolean.FALSE;
//            weixinUser.introduceCode = dcode;
//            weixinUser.city = wxMpUser.getCity();
//            weixinUser.country = wxMpUser.getCountry();
//            weixinUser.language = wxMpUser.getLanguage();
//            weixinUser.headImgUrl = wxMpUser.getHeadImgUrl();
//            weixinUser.isFirst = true;
//            if ("1".equals(wxMpUser.getSex()) || "男".equals(wxMpUser.getSex())) {
//                weixinUser.sex = Gender.MALE;
//            } else if ("2".equals(wxMpUser.getSex()) || "女".equals(wxMpUser.getSex())) {
//                weixinUser.sex = Gender.FEMALE;
//            } else {
//                weixinUser.sex = Gender.UNKNOWN;
//            }
//            weixinUser.subcribed = wxMpUser.getSubscribe();
            weixinUser.save();
        }
        weixinUser.save();
        return weixinUser;
    }

    /**
     * 查询出商户指定OpenId的用户.
     */
    public static User findOrCreateMerchantWxUser( WxMpUser wxMpUser , String dcode) {
        User weixinUser = User.find("openId=? and deleted = ?",wxMpUser.getOpenId() , DeletedStatus.UN_DELETED).first();
        if (weixinUser == null) {
            weixinUser = new User();
            weixinUser.createdAt = new Date();
            weixinUser.deleted = DeletedStatus.UN_DELETED;
            weixinUser.openId = wxMpUser.getOpenId();
            weixinUser.nickName = wxMpUser.getNickname();
            weixinUser.save();
        }
        weixinUser.save();
        return weixinUser;
    }

    public static Object findCampusName(String userId){
        String sql = "select a.name from bd_campuses a " +
                "left outer join bd_buildings b on a.id=b.campus_id " +
                "left outer join bd_rooms c on b.id=c.campus_building_id " +
                "left outer join co_contract_rooms d on c.id=d.campus_room_id " +
                "left outer join co_contracts e on d.contract_id=e.id " +
                "left outer join co_bank_cards f on e.id=f.contract_id " +
                "left outer join ur_users g on g.id=f.user_id " +
                "where g.id='"+userId+"' limit 1";
        if(em().createNativeQuery(sql).getResultList().size() == 0){
            return null;
        }else {
            return em().createNativeQuery(sql).getSingleResult();
        }
    }
}
